package com.me.web;

import com.me.pojo.Pokemon;
import com.me.pojo.Trainer;
import com.me.service.PokemonService;
import com.me.service.TrainerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/searchServlet")
public class searchServlet extends HttpServlet {
    //1.调用PokemonService获取所有Pokemon
    PokemonService service = new PokemonService();
    TrainerService trainerService = new TrainerService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.接收keyword
        request.setCharacterEncoding("utf-8");
        String keyword =request.getParameter("keyword");
        List<Pokemon> pokemons= service.searchpokemonBykeyword(keyword);
        List<Trainer> trainers= trainerService.searchTrainerBykeyword(keyword);
        //2.将Pokemon放入request
        request.setAttribute("pokemons",pokemons);
        request.setAttribute("trainers",trainers);
        //3.转发到搜索页面
        request.getRequestDispatcher("searchpokemon.jsp").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
